﻿// -----------------------------------------------------------------------
// <copyright file="DocumentData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITIES;
    
    /// <summary>
    /// Class access to db Document
    /// </summary>
    public class DocumentData
    {
        /// <summary>
        /// Get Document By Id
        /// </summary>
        /// <param name="documentId">Param documentId</param>
        /// <returns>Return Document</returns>
        public DocumentEN GetDocumentById(int documentId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                DocumentEN document = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneDocumentGetById, documentId);
                while (reader.Read())
                {
                    document = new DocumentEN();

                    if (!reader.IsDBNull(0))
                    {
                        document.DocumentId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        document.ProfileId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        document.UserId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        document.Date = reader.GetDateTime(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        document.Content = reader.GetString(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        document.Attachments = reader.GetString(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        document.DepartmentId = reader.GetInt32(6);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return document;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all Document
        /// </summary>
        /// <returns>Return list all Document</returns>
        public IList<DocumentEN> GetDocuments()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<DocumentEN> lstDocument = new List<DocumentEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneDocumentGetAll);
                while (reader.Read())
                {
                    DocumentEN document = new DocumentEN();

                    if (!reader.IsDBNull(0))
                    {
                        document.DocumentId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        document.ProfileId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        document.UserId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        document.Date = reader.GetDateTime(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        document.Content = reader.GetString(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        document.Attachments = reader.GetString(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        document.DepartmentId = reader.GetInt32(6);
                    }

                    lstDocument.Add(document);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstDocument;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Insert to db Document
        /// </summary>
        /// <param name="document">Paarm Document</param>
        /// <returns>Insert Document</returns>
        public int Insert(DocumentEN document)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.Document.ProfileId, document.ProfileId),
                                    new SqlParameter(Constants.SqlParamerterName.Document.UserId, document.UserId),
                                    new SqlParameter(Constants.SqlParamerterName.Document.Date, (object)document.Date ?? DBNull.Value),
                                    new SqlParameter(Constants.SqlParamerterName.Document.Content, document.Content),
                                    new SqlParameter(Constants.SqlParamerterName.Document.Attachments, document.Attachments),
                                    new SqlParameter(Constants.SqlParamerterName.Document.Departmentd, document.DepartmentId)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneDocumentInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Update to db Document
        /// </summary>
        /// <param name="document">Param Document</param>
        public void Update(DocumentEN document)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.Document.DocumentId, document.DocumentId),
                                    new SqlParameter(Constants.SqlParamerterName.Document.ProfileId, document.ProfileId),
                                    new SqlParameter(Constants.SqlParamerterName.Document.UserId, document.UserId),
                                    new SqlParameter(Constants.SqlParamerterName.Document.Date, (object)document.Date ?? DBNull.Value),
                                    new SqlParameter(Constants.SqlParamerterName.Document.Content, document.Content),
                                    new SqlParameter(Constants.SqlParamerterName.Document.Attachments, document.Attachments),
                                    new SqlParameter(Constants.SqlParamerterName.Document.Departmentd, document.DepartmentId)
                                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneDocumentUpdate, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete Document by documentId
        /// </summary>
        /// <param name="documentId">param documentId</param>
        public void Delete(int documentId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.Document.DocumentId, documentId)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneDocumentDelete, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get List Document By profileId
        /// </summary>
        /// <param name="profileId">Param profileId</param>
        /// <returns>Return List Document</returns>
        public IList<DocumentEN> GetDocumentByProfileId(int profileId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<DocumentEN> lstDocument = new List<DocumentEN>();
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneDocumentGetByProfileId, profileId);
                while (reader.Read())
                {
                    DocumentEN document = new DocumentEN();

                    if (!reader.IsDBNull(0))
                    {
                        document.DocumentId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        document.ProfileId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        document.UserId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        document.Date = reader.GetDateTime(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        document.Content = reader.GetString(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        document.Attachments = reader.GetString(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        document.DepartmentId = reader.GetInt32(6);
                    }

                    lstDocument.Add(document);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstDocument;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Gets the document by user_ document.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <param name="documentId">The document id.</param>
        /// <returns>return DocumentEN</returns>
        public DocumentEN GetDocumentByUser_Document(int userId, int documentId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                DocumentEN document = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneDocumentGetByUser_Document, userId, documentId);
                while (reader.Read())
                {
                    document = new DocumentEN();

                    if (!reader.IsDBNull(0))
                    {
                        document.DocumentId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        document.ProfileId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        document.UserId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        document.Date = reader.GetDateTime(3);
                    }

                    if (!reader.IsDBNull(4))
                    {
                        document.Content = reader.GetString(4);
                    }

                    if (!reader.IsDBNull(5))
                    {
                        document.Attachments = reader.GetString(5);
                    }

                    if (!reader.IsDBNull(6))
                    {
                        document.DepartmentId = reader.GetInt32(6);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return document;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}